package cn.enilu.guns.admin.modular.system.controller;

import cn.enilu.guns.admin.common.constant.device.AuthorizeConstant;
import cn.enilu.guns.admin.core.base.tips.AuthorizeErrorTip;
import cn.enilu.guns.bean.annotion.core.BussinessLog;
import cn.enilu.guns.bean.dictmap.CommonDict;
import cn.enilu.guns.service.door.EquipmentInfoService;
import cn.enilu.guns.utils.license.CheckAuthorizeCode;
import cn.enilu.guns.utils.license.EncoderFile;
import cn.enilu.guns.utils.license.LicenseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by JAVA on 2018/5/3.
 */
@Controller
@RequestMapping("/license")
public class LicenseController {

    private String PREFIX = "/system/license/";
    @Autowired
    private EquipmentInfoService equipmentInfoService;
    /**
     * 跳转到首页
     */
    @RequestMapping(value="",method = RequestMethod.GET)
    public String index(Model model) {
        try {
            Long count = equipmentInfoService.count();
            if (!CheckAuthorizeCode.AuthorizeCode(AuthorizeConstant.LICENSE_PATH,AuthorizeConstant.XM_NAME, AuthorizeConstant.XZQH,count)) {
                model.addAttribute("message","系统未授权，请联系客服获取授权码");
            }else{
                model.addAttribute("message",CheckAuthorizeCode.authorizeCountView(AuthorizeConstant.LICENSE_PATH,AuthorizeConstant.XM_NAME, AuthorizeConstant.XZQH,count));
            }
        } catch (Exception e) {
            model.addAttribute("message",e.getMessage());
            e.printStackTrace();
        }
        model.addAttribute("license",LicenseCode.getApplyCode(AuthorizeConstant.XM_NAME, AuthorizeConstant.XZQH));
        return PREFIX + "license.html";
    }
    /**
     * 单独的验证授权的方法，该方法中拿授权文件中的授权码，解密成一个
     * 系统名称+行政区划+硬盘序号+授权时间+授权截止时间串，
     * 然后获取本机的系统名称+行政区划+硬盘序号串，两者进行比较，如果验证通过，再判断时间是否
     * 到期，如果都通过，则返回验证通过。
     * 否则，使用本机的系统名称+行政区划+硬盘序号串，加密成一个申请码返回给前台
     * @return
     */
//    @CrossOrigin
//    @GetMapping(value= "" )
//    public Object license(){
//        Map resultMap = new HashMap();
//        try {
//            if (!CheckAuthorizeCode.AuthorizeCode(AuthorizeConstant.LICENSE_PATH,AuthorizeConstant.XM_NAME, AuthorizeConstant.XZQH,14)) {
//            if (CheckAuthorizeCode.AuthorizeCode(licensePath,xm_name, xzqh)) {
//                resultMap.put("success",true);
//                resultMap.put("msg","授权成功");
//                return resultMap;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        resultMap.put("success",false);
//        resultMap.put("msg","系统未授权，请授权后登录");
//        resultMap.put("license", LicenseCode.getApplyCode(AuthorizeConstant.XM_NAME, AuthorizeConstant.XZQH));
//        return resultMap;
//    }

    /**
     * 单独的授权码保存接口，拿到传入的授权码后，保存到服务器的一个文件中
     * @param authorizeCode
     * @return
     */
//    @CrossOrigin
    @RequestMapping(value = "/importlicense",method = RequestMethod.POST)
    @ResponseBody
    @BussinessLog(value = "授权", key = "authorizeCode",dict= CommonDict.class)
    public Object importlicense(@RequestParam(required = false) String authorizeCode){
        Map resultMap = new HashMap();
        File file = new File(AuthorizeConstant.LICENSE_PATH);
        if(!file.exists()){
            file.getParentFile().mkdirs();
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        EncoderFile.mywrite(file,authorizeCode);
        boolean flag = false;
        try {
            flag = CheckAuthorizeCode.AuthorizeCode(AuthorizeConstant.LICENSE_PATH,AuthorizeConstant.XM_NAME, AuthorizeConstant.XZQH); //验证授权码是否正确
        } catch (Exception e) {
            e.printStackTrace();
//            resultMap.put("success",false);
//            resultMap.put("msg","授权失败，"+e.getMessage());
//            return resultMap;
            return new AuthorizeErrorTip("授权失败，"+e.getMessage());
        }
        if(flag){ //授权成功
            resultMap.put("success",true);
            resultMap.put("msg","授权成功");
            return resultMap;
        }
        return new AuthorizeErrorTip("授权码错误");
    }



}
